package com.yanq.liancar.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yanq.liancar.R;
import com.yanq.liancar.bean.ServiceTypeBean;
import com.yanq.liancar.util.LogUtils;

import java.io.File;
import java.util.List;

public class GvServiceImgAdapter extends BaseAdapter {
    private Context context;
    private List<File> datas;
    private LayoutInflater layoutInflater;

    public int getMaxImages() {
        return maxImages;
    }

    public void setMaxImages(int maxImages) {
        this.maxImages = maxImages;
    }

    private int maxImages = 5;

    public GvServiceImgAdapter(Context context, List<File> list) {

        this.context = context;

        this.datas = list;

        layoutInflater = LayoutInflater.from(context);

    }
    @Override
    public int getCount() {

        int count = datas == null ? 1 : datas.size() + 1;
        if (count >= maxImages) {
            return datas.size();
        } else {
            return count;
        }

    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        LogUtils.e("shhhfhd",position+"");
        if (convertView==null){
            holder = new ViewHolder();
            convertView=layoutInflater.inflate(R.layout.item_service_img,parent,false);
            holder.addimg=convertView.findViewById(R.id.addimg);
            holder.del_img=convertView.findViewById(R.id.img_del);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position<datas.size()){
            Glide.with(context).load(datas.get(position)).into(holder.addimg);
            holder.del_img.setVisibility(View.VISIBLE);
        }else {
            Glide.with(context).load(R.mipmap.service_add_img).into(holder.addimg);
            holder.del_img.setVisibility(View.GONE);
        }
        return convertView;
    }

    static class ViewHolder{
        ImageView addimg;
        ImageView del_img;
    }

}
